package logic

import (
	"context"
	"time"

	"mall/common/errorx"
	"mall/service/backend/model"
	"mall/service/backend/rpc/internal/svc"
	"mall/service/backend/rpc/types/backend"

	"github.com/zeromicro/go-zero/core/logx"
)

type SetUserStatusLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSetUserStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SetUserStatusLogic {
	return &SetUserStatusLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SetUserStatusLogic) SetUserStatus(in *backend.SetUserStatusRequest) (*backend.SetUserStatusResponse, error) {
	if len(in.Ids) == 0 {
		return nil, errorx.Status(errorx.ERROR_USER_ID_REQUIRED, "")
	}

	_, err := l.svcCtx.BackendUser.UpdateBatch(l.ctx, in.Ids, model.M{
		model.BackendUserColumns.Status:        uint16(in.Status),
		model.BackendUserColumns.DelStatus:     uint16(in.Status),
		model.BackendUserColumns.LastUpdatedBy: uint64(in.UpdatedBy),
		model.BackendUserColumns.UpdatedAt:     time.Now(),
	})
	if err != nil && err != model.ErrNotUpdated {
		return nil, err
	}

	return &backend.SetUserStatusResponse{}, nil
}
